#!/bin/bash
which node
node -v
#which yarn
#yarn -v
DATES=`date +%Y%m%d%H%M`
DATE=$DATES
DATA=`pwd`
cd $DATA

# 为node modules做缓存， 有缓存用缓存，没有则你npm install并添加缓存
PACKAGE_HASH=$(md5sum package.json | awk '{print $1}');
mkdir -p ~/builds/cache/node_modules # 使用docker模式时需要配置volume 保证缓存起作用 在后面runner部分会提到。
NPM_CACHE=~/builds/cache/node_modules/${PACKAGE_HASH}.tar
if [ -f $NPM_CACHE ];
then
  echo "Use Cache";
  tar xf $NPM_CACHE;
else
  cnpm install;
  if [ $? -ne 0 ]; then
    echo "cnpm install failed"
    exit
  else
    tar cf - ./node_modules > $NPM_CACHE;
  fi
fi

#build () {
PATH=$PATH:./node_modules/.bin
npm run build:prod
if [ $? -ne 0 ]; then
    echo "npm run build failed"
    exit 1
else
    NAME=vote-admin-web
    echo "npm run build"
    docker build -t gpt.com/chatgpt-manage/${NAME}:$DATE .
    echo "docker build ${NAME}:$DATE"
#    docker push test.docker.seonpower.com/box-server-test/${NAME}:$DATE
#    echo "docker push no-code-admin:$DATE "
#    echo "${NAME}:$DATE" >> version.txt
#    echo "docker stop ${NAME}"
#    docker stop ${NAME}
#    echo "docker rm ${NAME}"
#    docker rm ${NAME}
#    echo "docker run -it --name=${NAME} -p 10090:80 -d gpt.com/chatgpt-manage/${NAME}:$DATE && docker exec -d nginx nginx -s reload"
#    docker run -it --name=${NAME} -p 10092:80 -d gpt.com/chatgpt-manage/${NAME}:$DATE    echo "docker run -it --name=no-code-admin --network no-code -d test.docker.seonpower.com/box-server-test/no-code-admin:$DATE && docker exec -d nginx nginx -s reload"

    kubectl set image deployment/${NAME} ${NAME}=youzan.com/vote-saas/${NAME}:$DATE -n vote-server
    echo "更新成功，版本号：$DATE"
fi
